﻿namespace JxcLite.Pages.Finance;

/// <summary>
/// 对账单列表。
/// </summary>
public class AccountList : BaseTablePage<JxAccountHead>
{
    private IFinanceService Service;

    /// <summary>
    /// 取得对账类型（客户、供应商）。
    /// </summary>
    protected virtual string Type { get; }

    protected override async Task OnInitPageAsync()
    {
        await base.OnInitPageAsync();
        Service = await CreateServiceAsync<IFinanceService>();
        Table.Toolbar.ShowCount = 6;
        Table.FormType = typeof(AccountForm);
        Table.OnQuery = QueryAccountsAsync;
        Table.Column(c => c.Status).Template((b, r) => b.Tag(r.Status));
        Table.Column(c => c.AccountDate).Type(FieldType.Date);
    }

    public async void New()
    {
        var row = await Service.GetDefaultAccountAsync(Type);
        Table.NewForm(Service.SaveAccountAsync, row);
    }

    public void DeleteM() => Table.DeleteM(Service.DeleteAccountsAsync);
    public void Edit(JxAccountHead row) => Table.EditForm(Service.SaveAccountAsync, row);
    public void Delete(JxAccountHead row) => Table.Delete(Service.DeleteAccountsAsync, row);
    public async void Export() => await Table.ExportDataAsync();

    private Task<PagingResult<JxAccountHead>> QueryAccountsAsync(PagingCriteria criteria)
    {
        criteria.SetQuery(nameof(JxAccountHead.Type), QueryType.Equal, Type);
        return Service.QueryAccountsAsync(criteria);
    }
}